﻿/* 
 * Copyright (c) 2010 Letwory Interactive
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * -- Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * -- Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * Original contributor: Nathan Letwory
 */

using System;

namespace VerseSharp
{
    public delegate void GeometryBoneCreateCallback(GeometryBoneCreateCommand command);
    public delegate void GeometryBoneDestroyCallback(GeometryBoneCreateCommand command);

    public class GeometryBoneCreateCommand : IVerseCommand
    {
        public CommandID ID { get { return CommandID.GeometryBoneCreate; } }
        public uint NodeID;
        public ushort BoneID;
        /// <summary>
        /// Name of the weight layer. Maximum 16 bytes long. Set to null if the bone is destroyed.
        /// Names longer than 16 are truncated.
        /// </summary>
        private string _weight;
        public string Weight
        {
            get { return _weight; }
            set
            {
                if (value != null)
                {
                    if (value.Length <= 16)
                    {
                        _weight = value;
                    }
                    else
                    {
                        _weight = value.Substring(0, 16);
                    }
                }
            }
        }
        private string _reference;
        public string Reference
        {
            get { return _reference; }
            set
            {
                if (value != null)
                {
                    if (value.Length <= 16)
                    {
                        _reference = value;
                    }
                    else
                    {
                        _reference = value.Substring(0, 16);
                    }
                }
            }
        }
        public ushort Parent;
        public double PosX;
        public double PosY;
        public double PosZ;
        private string _position_label;
        public string PositionLabel
        {
            get { return _position_label; }
            set
            {
                if (value != null)
                {
                    if (value.Length <= 16)
                    {
                        _position_label = value;
                    }
                    else
                    {
                        _position_label = value.Substring(0, 16);
                    }
                }
            }
        }
        private string _rotation_label;
        public string RotationLabel
        {
            get { return _rotation_label; }
            set
            {
                if (value != null)
                {
                    if (value.Length <= 16)
                    {
                        _rotation_label = value;
                    }
                    else
                    {
                        _rotation_label = value.Substring(0, 16);
                    }
                }
            }
        }
        private string _scale_label;
        public string ScaleLabel
        {
            get { return _scale_label; }
            set
            {
                if (value != null)
                {
                    if (value.Length <= 16)
                    {
                        _scale_label = value;
                    }
                    else
                    {
                        _scale_label = value.Substring(0, 16);
                    }
                }
            }
        }

        public void Deserialize(byte[] source, ref int pos)
        {
            NodeID = Utils.GetUInt32(source, ref pos);
            BoneID = Utils.GetUInt16(source, ref pos);
            _weight = Utils.GetString(source, 16, ref pos);
            _reference = Utils.GetString(source, 16, ref pos);
            Parent = Utils.GetUInt16(source, ref pos);
            PosX = Utils.GetReal64(source, ref pos);
            PosY = Utils.GetReal64(source, ref pos);
            PosZ = Utils.GetReal64(source, ref pos);
            _position_label = Utils.GetString(source, 16, ref pos);
            _rotation_label = Utils.GetString(source, 16, ref pos);
            _scale_label = Utils.GetString(source, 16, ref pos);
        }

        public void Serialize(byte[] destination, ref int pos)
        {
            Utils.PutUInt32(destination, ref pos, NodeID);
            Utils.PutUInt16(destination, ref pos, BoneID);
            Utils.PutString(destination, _weight, 16, ref pos);
            Utils.PutString(destination, _reference, 16, ref pos);
            Utils.PutUInt16(destination, ref pos, Parent);
            Utils.PutReal64(destination, ref pos, PosX);
            Utils.PutReal64(destination, ref pos, PosY);
            Utils.PutReal64(destination, ref pos, PosZ);
            Utils.PutString(destination, _position_label, 16, ref pos);
            Utils.PutString(destination, _rotation_label, 16, ref pos);
            Utils.PutString(destination, _scale_label, 16, ref pos);
        }
    }
}
